package com.example.yaoalarm;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    private TextView thetime;
    private Switch thebtn;
    private AlarmManager am;
    private PendingIntent pendingIntent;
    private Intent intent;
    private int cgdhour = 7;
    private int cgdmin = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        thetime= this.findViewById(R.id.setm);
        thebtn=this.findViewById(R.id.swh_status);
        thebtn.setChecked(false);

        am = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
        intent = new Intent(MainActivity.this, ring.class);
        pendingIntent = PendingIntent.getActivity(this,0,intent,0);

        thetime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(MainActivity.this, selecttime.class);
//                startActivity(intent1);
                startActivityForResult(intent1,10);
            }
        });
        thebtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    if(isoutdue(cgdhour,cgdmin) == 1)
                        Toast.makeText(MainActivity.this,"闹钟已开启,将于明日"+thetime.getText()+"响铃",Toast.LENGTH_SHORT).show();
                    else
                        Toast.makeText(MainActivity.this,"闹钟已开启,将于今日"+thetime.getText()+"响铃",Toast.LENGTH_SHORT).show();
                    setAlarm();
                }else {
                    Toast.makeText(MainActivity.this,"闹钟已关闭",Toast.LENGTH_SHORT).show();
                    cancelAlarm();
                }
            }
        });
    }

    //判断用户设定的时间在今天是否已经超时
    public int isoutdue(int a, int b){
        //获得Calendar这个类的实例
        Calendar ca=Calendar.getInstance();
        if(ca.get(Calendar.HOUR_OF_DAY) > a ||(ca.get(Calendar.HOUR_OF_DAY) == a && ca.get(Calendar.MINUTE) > b))
            return 1;
        else
            return 0;
    }

    //设置时间：long startTime
    public long getTimeDiff(int a, int b){
        //获得Calendar这个类的实例
        Calendar ca=Calendar.getInstance();
        //注意精确到秒，时间可以自由设置
        //设置为明日
        if(isoutdue(a,b) == 1){
            ca.set(Calendar.DATE,ca.get(Calendar.DATE)+1);
            ca.set(Calendar.HOUR_OF_DAY,a);
            ca.set(Calendar.MINUTE,b);
            ca.set(Calendar.SECOND,0);
        }
        else {
            ca.set(Calendar.HOUR_OF_DAY,a);
            ca.set(Calendar.MINUTE,b);
            ca.set(Calendar.SECOND,0);
        }
        return ca.getTimeInMillis();
    }

    //开启闹钟
    private void setAlarm(){
        //android Api的改变不同版本中设 置有所不同
        if(Build.VERSION.SDK_INT<19){
            am.set(AlarmManager.RTC_WAKEUP,getTimeDiff(cgdhour,cgdmin),pendingIntent);
        }else{
            am.setExact(AlarmManager.RTC_WAKEUP,getTimeDiff(cgdhour,cgdmin),pendingIntent);
        }
    }

    //关闭闹钟
    private void cancelAlarm(){
        am.cancel(pendingIntent);
    }

    //与子activity交互，获得用户设置的时间数据
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(10 == requestCode){
            Bundle bundle=data.getExtras();
            if(bundle.getInt("flag") == 1){
                cgdhour = bundle.getInt("hour");
                cgdmin = bundle.getInt("minute");
                if(cgdhour < 10 && cgdmin < 10)
                    thetime.setText("0"+cgdhour+":0"+cgdmin);
                else if(cgdhour < 10)
                    thetime.setText("0"+cgdhour+":"+cgdmin);
                else if(cgdmin < 10)
                    thetime.setText(cgdhour+":0"+cgdmin);
                else
                    thetime.setText(cgdhour+":"+cgdmin);
            }
        }
    }
}